package com.tx.studentManager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tx.studentManager.config.SystemConfig;
import com.tx.studentManager.model.Page;
import com.tx.studentManager.model.Role;
import com.tx.studentManager.qo.RoleQO;
import com.tx.studentManager.service.RoleService;
import com.tx.studentManager.utils.DWZJsonUtil;

/**
 * 
* @描述 角色管理控制器
* @since studentManager-admin 1.0
* @version 1.0 2015-9-8 下午2:35:30
* @author 此生唯晴
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController {
	private Logger logger=Logger.getLogger(RoleController.class);
	@Autowired
	private RoleService roleService;
	@Autowired
	private SystemConfig systemConfig;
	/**
	 * 
	* @描述 查询所有角色
	* @param qo
	* @param pageSize
	* @param pageNo
	* @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(RoleQO qo,
			@RequestParam(required = false, value = "numPerPage") Integer pageSize,
			@RequestParam(required = false, value = "pageNum") Integer pageNo){
		ModelAndView modelAndView=new ModelAndView();
		Page page=new Page();
		page.setCondition(qo);
		pageSize = pageSize == null ? systemConfig.getQueryPageSize() : pageSize;
		pageNo = pageNo == null ? 1 : pageNo;
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page=roleService.list(page);
		modelAndView.addObject("page", page);
		modelAndView.setViewName("admin/system/role/roleList");
		return modelAndView;
	}
	
	@RequestMapping("/add")
	public String add(){
		
		return "admin/system/role/roleAdd";
	}
	
	/**
	 * 
	* 添加角色
	* @param role
	* @return
	 */
	@ResponseBody
	@RequestMapping(value="/toAddRole",produces = "text/html;charset=UTF-8")
	public String toAddRole(Role role){
		if (StringUtils.isBlank(role.getDescription())) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "角色名不能为空", "", null);
		}
		//检查角色是否已存在
		String checkHql="from Role where description=:description or roleName=:roleName";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("description", role.getDescription());
		params.put("roleName", role.getRoleName());
		List<Role>list=roleService.selectByHQL(checkHql, params);
		if (list.size()>0) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "该角色已存在", null, null);
		}
		role=roleService.insert(role);
		if (role!=null) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "添加成功", DWZJsonUtil.OPER_CLOSE_CURRENT, "roleManager");
		}else{
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "添加失败", DWZJsonUtil.OPER_CLOSE_CURRENT, null);
		}
	}
	
	/**
	 * 
	* @描述 打开修改页面
	* @param roleId
	* @param model
	* @return
	 */
	@RequestMapping("/modifyRole")
	public String modifyRole(Integer roleId,Model model){
		Role role=roleService.findUniqueBy("roleId", roleId);
		model.addAttribute("role", role);
		return "admin/system/role/roleUpdate";
	}
	
	/**
	 * 
	* @描述 修改角色
	* @param role
	* @return
	 */
	@ResponseBody
	@RequestMapping(value="/toModifyRole",produces = "text/html;charset=UTF-8")
	public String toModifyRole(Role role){
		boolean result=roleService.update(role);
		if (result) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "修改成功", DWZJsonUtil.OPER_CLOSE_CURRENT, "roleManager");
		}else{
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "修改失败", DWZJsonUtil.OPER_CLOSE_CURRENT, null);
		}
	}
	
	/**
	 * 
	* @描述 根据角色ID删除角色，有关联用户时（异常）不能删除
	* @param roleId
	* @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteRole",produces = "text/html;charset=UTF-8")
	public String deleteRole(Integer roleId){
		 String sql="delete from role where roleId=?";
		 Object []params=new Object[1];
		 params[0]=roleId;
		 int result=0;
		 try {
			result=roleService.batchExecuteWithSql(sql, params);
		} catch (DataIntegrityViolationException e) {
			//e.printStackTrace();
			logger.error("存在关联此角色的用户，不允许删除");
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "该角色存在关联用户，不允许删除", null, null);
		}
		 if (result>0) {
			 return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "删除成功", null, "roleManager");
		}
		 return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "删除失败", null, null);
	}
}
